package com.yiqi.liebang.feature.order.view;


import android.os.Parcelable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;

import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.SlidingTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.luck.picture.lib.rxbus2.RxBus;
import com.luck.picture.lib.rxbus2.Subscribe;
import com.luck.picture.lib.rxbus2.ThreadMode;
import com.suozhang.framework.component.http.RxDataProcessFactory;
import com.suozhang.framework.framework.AM;
import com.suozhang.framework.framework.BaseFragment;
import com.yiqi.liebang.R;
import com.yiqi.liebang.common.util.EventEntity;
import com.yiqi.liebang.common.widget.TabEntity;
import com.yiqi.liebang.entity.bo.UnReadBo;
import com.yiqi.liebang.framework.api.HomeApi;

import java.util.ArrayList;

import butterknife.BindView;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * A simple {@link Fragment} subclass.
 */
public class OrderQuestionFragment extends BaseFragment {

    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();
    @BindView(R.id.tl_2)
    CommonTabLayout mTabLayout_2;
    @BindView(R.id.vp)
    ViewPager mVp;

    private MyPagerAdapter mAdapter;

    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private final String[] mTitles = {
            "进行中", "待评价", "已完成", "已收到"
    };

    @Override
    public int attachLayoutRes() {
        return R.layout.fragment_question_order;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    public void onStart() {
        super.onStart();
        RxBus.getDefault().register(this);
        RxBus.getDefault().post(new EventEntity(3, 2));
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }


//    @Subscribe(threadMode = ThreadMode.MAIN)
//    public void eventBus(EventEntity obj) {
//        switch (obj.what) {
//            case 9:
//                getUnread();
//                break;
//        }
//    }

    @Override
    protected void initView() {

        tl_2();


    }

    private void tl_2() {
        mFragments=new ArrayList<>();
        for (int i = 0; i < mTitles.length; i++) {
            mFragments.add(QuestionListFragment.getInstance(i + 1));
        }

        mAdapter = new MyPagerAdapter(getChildFragmentManager());
        mVp.setAdapter(mAdapter);
        mTabEntities=new ArrayList<>();
        for (int i = 0; i < mTitles.length; i++) {
            mTabEntities.add(new TabEntity(mTitles[i], R.drawable.white_radius, R.drawable.white_radius));
        }
        mTabLayout_2.setTabData(mTabEntities);
        mTabLayout_2.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                mVp.setCurrentItem(position);
            }

            @Override
            public void onTabReselect(int position) {

            }
        });

        mVp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                mTabLayout_2.setCurrentTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        mVp.setCurrentItem(0);
        getUnread();
    }


    private void getUnread() {
        AM.api().createApiService(HomeApi.class)
                .getUnreadNUm(0)
                .compose(RxDataProcessFactory.dataPrepAndIoToMainTransformer())
                .subscribe(new Observer<UnReadBo>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(UnReadBo unReadBo) {
                        if (unReadBo == null) return;
                        try {
                            if (unReadBo.getUnreadNum1() > 0) {
                                mTabLayout_2.showMsg(0, unReadBo.getUnreadNum1());
                                mTabLayout_2.setMsgMargin(0, -62, 0);
                            }
                            if (unReadBo.getUnreadNum2() > 0) {
                                mTabLayout_2.showMsg(1, unReadBo.getUnreadNum2());
                                mTabLayout_2.setMsgMargin(1, -62, 0);
                            }
                            if (unReadBo.getUnreadNum3() > 0) {
                                mTabLayout_2.showMsg(2, unReadBo.getUnreadNum3());
                                mTabLayout_2.setMsgMargin(2, -62, 0);
                            }
                            if (unReadBo.getUnreadNum4() > 0) {
                                mTabLayout_2.showMsg(3, unReadBo.getUnreadNum4());
                                mTabLayout_2.setMsgMargin(3, -62, 0);
                            }
                        }catch (Exception e){

                        }



                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initEvent() {

    }

    private class MyPagerAdapter extends FragmentStatePagerAdapter {
        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTitles[position];
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }

        @Override
        public void restoreState(Parcelable state, ClassLoader loader) {

        }
    }
}
